package com.yunshang.ysmall.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yunshang.ysmall.mapper.MbAddrInfoMapper;
import com.yunshang.ysmall.mapper.MemberInfoMapper;
import com.yunshang.ysmall.mapper.OrderInfoMapper;
import com.yunshang.ysmall.mapper.OrderItemInfoMapper;
import com.yunshang.ysmall.mapper.ProductInfoMapper;
import com.yunshang.ysmall.mapper.ReturnFanchangInfoMapper;
import com.yunshang.ysmall.model.MbAddrInfo;
import com.yunshang.ysmall.model.MemberInfo;
import com.yunshang.ysmall.model.OrderInfo;
import com.yunshang.ysmall.model.OrderItemInfo;
import com.yunshang.ysmall.model.ProductInfo;
import com.yunshang.ysmall.model.ReturnFanchang;
import com.yunshang.ysmall.service.MemberService;
@Service
public class MemberServiceImpl implements MemberService {
	@Resource
	MemberInfoMapper memberInfoMapper;//会员表
	@Resource
	MbAddrInfoMapper mbAddrInfoMapper;//地址表
	@Resource
	OrderInfoMapper orderInfoMapper;//订单信息表
	@Resource
	OrderItemInfoMapper orderItemInfoMapper;//订单详情列表
	@Resource
	ProductInfoMapper productInfoMapper;
	@Resource
	ReturnFanchangInfoMapper  returnFanchangeInfoMapper;

	@Override
	public void loginHandle(MemberInfo memberInfo) {
		// TODO Auto-generated method stub
		if(memberInfoMapper.selectByMemberId(memberInfo.getMemberId()) != null){
			memberInfoMapper.update(memberInfo);
		}else{
			memberInfoMapper.insert(memberInfo);
		}
	}

	@Override
	public MemberInfo getMemberById(String memberId) {
		// TODO Auto-generated method stub
		return memberInfoMapper.selectByMemberId(memberId);
	}

	@Override
	public boolean doPay(MemberInfo memberInfo) {
		// TODO Auto-generated method stub
		return memberInfoMapper.dopay(memberInfo) > 0 ? true : false;
	}

	@Override
	public boolean addMbAddr(MbAddrInfo mbAddrInfo) {
		// TODO Auto-generated method stub
		return mbAddrInfoMapper.insert(mbAddrInfo) > 0 ? true : false;
	}

	@Override
	public boolean delMbAddr(Integer addrId) {
		// TODO Auto-generated method stub
		return mbAddrInfoMapper.deleteByPrimaryKey(addrId) > 0 ? true : false;
	}

	@Override
	public MbAddrInfo getAddrByMemberId(String memberId) {
		// TODO Auto-generated method stub
		return mbAddrInfoMapper.selectByMemberId(memberId);
	}

	@Override
	public boolean saveOrder(OrderInfo orderInfo) {
		// TODO Auto-generated method stub
		return orderInfoMapper.insert(orderInfo) > 0 ? true : false;
	}

	@Override
	public boolean updateOrder(OrderInfo orderInfo) {
		// TODO Auto-generated method stub
		return orderInfoMapper.update(orderInfo) >0 ? true : false;
	}

	@Override
	public boolean saveOrderItem(OrderItemInfo orderItemInfo) {
		// TODO Auto-generated method stub
		return orderItemInfoMapper.insert(orderItemInfo) >0 ? true : false;
	}
	
	//查询订单信息
	@Override
	public List<OrderInfo> getOrderListByParams(Map params) {
		//获取订单信息的集合
		List<OrderInfo> orderList = orderInfoMapper.getOrderListByParams(params);
		for (OrderInfo orderInfo : orderList) {
			//根据订单信息编号去找对应的订单详细信息，并且将数据封装到指定的订单信息中
			orderInfo.setItemList(orderItemInfoMapper.getItemListByOrderId(orderInfo.getOrderId()));
		}
		return orderList;
	}

	@Override
	public List<OrderItemInfo> getOrderItemListByOrderId(String orderId) {
		// TODO Auto-generated method stub
		return orderItemInfoMapper.getItemListByOrderId(orderId);
	}

	@Override
	public OrderInfo getOrderInfoById(String orderId) {
		// TODO Auto-generated method stub
		return orderInfoMapper.selectByOrderId(orderId);
	}

	@Override
	public MbAddrInfo getAddrById(Integer mbAddrId) {
		// TODO Auto-generated method stub
		return mbAddrInfoMapper.selectById(mbAddrId);
	}

	@Override
	public Integer getListCountByParams(Map params) {
		// TODO Auto-generated method stub
		return orderInfoMapper.getListCountByParams(params);
	}
	
	//后台会员时间显示
	@Override
	public List<MemberInfo> getMemberList(Map params){
		List<MemberInfo> memberInfos=memberInfoMapper.memberAll(params);
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (MemberInfo memberInfo : memberInfos) {
			memberInfo.setLastUtsTime(simpleDateFormat.format(memberInfo.getLastUpdTs()));
		}
		return	memberInfos;
	}

	@Override
	public MbAddrInfo showAddress(String memberId) {
		return mbAddrInfoMapper.selectByMemberId(memberId);
	}

	@Override
	public void updateAddress(MbAddrInfo mbAddrInfo) {
		mbAddrInfoMapper.updateMemberAddress(mbAddrInfo);
	}

	@Override
	public OrderInfo selectOrderinfo(String orderId) {
		return orderInfoMapper.selectByOrderId(orderId);
	}

	@Override
	public int getCount(String memberId) {
		return orderInfoMapper.orderListCount(memberId);
	}

	@Override
	public void getProductId(String productid,int soldCount) {
				ProductInfo productInfo=productInfoMapper.findProductId(productid);
				productInfo.setSoldCount(productInfo.getSoldCount()+soldCount);
				//通过产品id去修改其中的字段
				Map< String,Object> map=new HashMap<String, Object>();
				map.put("soldCount",productInfo.getSoldCount() );
				map.put("productid",productid);
				productInfoMapper.updateBySoldCount(map);
	}

	@Override
	public void saveSalesReturnInformation(ReturnFanchang returnFanchang) {
		
		returnFanchangeInfoMapper.savaSalesReturnInformaion(returnFanchang);
	}

	@Override
	public void updateOrderStatus(String orderId) {
		orderInfoMapper.updateOrderstatusAndTransferStatus(orderId);
		
	}


	
	@Override
	public List<OrderInfo> findOrderList(Map<String, Object> params) {
		return orderInfoMapper.findOrderListByParams(params);
	}

	@Override
	public int getCountOrderList(String attribute) {
		return orderInfoMapper.getCountOrderByMemberIdList(attribute);
	}

	@Override
	public void updateBonusCode(Map< String,Object> parameters) {
		   orderInfoMapper.updateOrderBonusCode(parameters);
	}



}
